package com.jt3g.system.service.impl;

import com.jt3g.redis.RedisUtils.RedisUtil;
import com.jt3g.system.mapper.SysBlackListIpMapper;
import com.jt3g.system.service.ISysBlackListIpService;
import com.jt3g.system.domain.SysBlackListIp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysBlackListIpServiceImpl implements ISysBlackListIpService {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private SysBlackListIpMapper sysBlackListIpMapper;
    @Override
    public SysBlackListIp findByIp(String ip) {
        List<SysBlackListIp> SysBlackListIps =(List<SysBlackListIp>)redisUtil.get("SysBlackListIps");
        if(SysBlackListIps==null ||SysBlackListIps.size()==0)
        {
            SysBlackListIp sysBlackListIp=new SysBlackListIp();
            SysBlackListIps=selectBlackListIpList(sysBlackListIp);
            redisUtil.set("SysBlackListIps",SysBlackListIps);
            List<SysBlackListIp> retrunlist=  SysBlackListIps.stream()
                    .filter(item -> item.getIpaddress().equals(ip))
                    .collect(Collectors.toList());
            return retrunlist.size()==0?null:retrunlist.get(0);
        }
       return sysBlackListIpMapper.findByIp(ip);
    }

    @Override
    public int addBlackList(SysBlackListIp blackList) {

        return sysBlackListIpMapper.addBlackList(blackList);
    }

    @Override
    public  List<SysBlackListIp> selectBlackListIpList(SysBlackListIp blackList) {

        return sysBlackListIpMapper.selectBlackListIpList(blackList);
    }
}
